﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xTeam.HengLe.DataAccess;
using System.Web.Mvc;
using System.ComponentModel;
using xTeam.HengLe.Entity;

namespace xTeam.HengLe.Controller.Manage
{
    [Authorize]
    public class LeaveWordController : BaseController
    {
        public LeaveWordController()
        {
            // 数据连接字符串
            LeaveWordReplyDataAccess.Con = _connectionString;
        }

        public ActionResult Index([DefaultValue(1)]int pageid)
        {
            int total = 0;
            List<ELeaveWord> leaveWordList = LeaveWordReplyDataAccess.GetLeaveWordReply(10, pageid, 1, ref total);
            ViewData["Pager"] = this.CreatePager(Url.Action("Index", new { pageid = "{0}" }), total, pageid);
            return View(leaveWordList);
        }
        /// <summary>
        /// 更新留言状态
        /// </summary>
        /// <param name="lid">留言ID</param>
        /// <param name="isvalid">有效，无效</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetState([DefaultValue(0)]int lid, [DefaultValue(true)]bool isvalid)
        {
            LeaveWordReplyDataAccess.UpdateLeaveWordState(lid, isvalid);
            return new EmptyResult();
        }
        /// <summary>
        /// 删除留言或回复
        /// </summary>
        /// <param name="lid">留言ID或回复ID</param>
        /// <param name="flag">标识：true删除留言，false删除回复</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete([DefaultValue(0)]int lid, [DefaultValue(true)]bool flag)
        {
            if (lid > 0)
            {
                if (flag)
                {
                    LeaveWordReplyDataAccess.DeleteLeaveWord(lid);
                }
                else
                {
                    LeaveWordReplyDataAccess.DeleteReply(lid);
                }
            }
            return new EmptyResult();
        }
        /// <summary>
        /// 添加或修改回复
        /// </summary>
        /// <param name="rid">回复ID</param>
        /// <param name="lid">留言ID</param>
        /// <param name="content">回复内容</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditReply([DefaultValue(0)]int rid, [DefaultValue(0)]int lid, [DefaultValue("")]string content)
        {
            if (lid > 0 && !string.IsNullOrEmpty(content))
            {
                ELeaveWord reply = new ELeaveWord
                {
                    RId = rid,
                    LId = lid,
                    Name = Convert.ToString(Session["admin"]),
                    Content = content
                };
                LeaveWordReplyDataAccess.EditReply(reply);
            }
            return new EmptyResult();
        }
    }
}
